
package za.ac.cput.peterjohn.DesignPatternPrincipals.Structural.Flyweight.Factory;

import java.util.HashMap;
import java.util.Map;
import za.ac.cput.peterjohn.DesignPatternPrincipals.Structural.Flyweight.Model.Flyweight;
import za.ac.cput.peterjohn.DesignPatternPrincipals.Structural.Flyweight.Model.FlyweightAdder;
import za.ac.cput.peterjohn.DesignPatternPrincipals.Structural.Flyweight.Model.FlyweightMultiplier;


public class FlyweightFactory 
{
    private static FlyweightFactory flyweightFactory;


    private static  Map<String, Flyweight> flyweightPool;



    public FlyweightFactory() {  flyweightPool = new HashMap<String, Flyweight>(); }
    
    public static FlyweightFactory getInstance() 
    {
        if(flyweightFactory == null)
            flyweightFactory = new FlyweightFactory();
        return flyweightFactory;
    }
    
    public static Flyweight getFlyweight(String key)
    {
        if(flyweightPool.containsKey(key))
            return flyweightPool.get(key);
        else
        {
            Flyweight flyweight;
            if("add".equals(key))
                flyweight = new FlyweightAdder();
            else
                flyweight = new FlyweightMultiplier();
            
            flyweightPool.put(key, flyweight);
            return flyweight;
        } 
    }
}
